<?php
/************************FoxPHP威客系统************************/
/*                                                            */
/*  FoxPHP.Com (C)2006-2016 FoxPHP Inc.                       */
/*  This is NOT a freeware, use is subject to license terms   */
/*  FoxPHP威客系统是商业软件,使用于商业用途请购买授权         */
/*  V3.0 2016.3                                               */
/*  foxphp@aliyun.com                                         */
/*  http://www.foxphp.com                                     */
/*                                                            */
/**************************************************************/

namespace Admin\Controller;
use Think\Controller;
class SqlbackupController extends IndexController {
	
	public function GetMysqlTableNameArray(){
		$tnamelen=strlen(C('DB_PREFIX'));
		$model = M();
		return $model->query('SHOW TABLE STATUS FROM '.C('DB_NAME').' WHERE "'.C('DB_PREFIX').'"=substring(Name,1,'.$tnamelen.')'); 
	}

	public function indexAction(){
		
		$list=$this->GetMysqlTableNameArray();
							   
		$nowdate=@date("Ymd");
	   
		$backupname=@date("Ymd").'_'.RandomText(8,1);
		
		$this->assign('list',$list);
		
		$this->assign('nowdate',$nowdate);
		
		$this->assign('backupname',$backupname);
		
		$this->display();
	}
	
	public function restoreAction(){
		$this->display();
	}
	
	public function levelAction(){
		
		
		if($_POST['backupname']){
			
			$backupname=@date("Ymd").'_'.RandomText(8,1).'.sql';

			$times =@date("Ymd");

			$filedir=C('S_BACKUP_DIR').'/'.$times;

			if(!is_dir($filedir)) {
				@mkdir($filedir,0777);  
				WriteFile($filedir.'/index.html','');
			}

			$dumpFileName=$filedir.'/'.$backupname;

			$result=exec("mysqldump C('DB_NAME') -u C('DB_USER') -p C('DB_PWD') > $dumpFileName");
			
			if($result){
				$this->error('备份数据成功路径 '.$dumpFileName,'/admin/'.CONTROLLER_NAME.'/index',10);
			}else{
				$this->error('备份失败,您的服务器环境配置不支持！','/admin/'.CONTROLLER_NAME.'/index',3);
			}
		}else{
			$nowdate=@date("Ymd");

			$this->assign('nowdate',$nowdate);
			
			$this->display();
		}
	}
	
	public function postAction(){
			
		include_once(Apps.'/Admin/Class/FoxphpBackup.php');
		
		$backup=new \BackupSQL();
		
		
		if($_GET['value']){
					  
			$number=$backup->SaveTableData(intval($_GET['value']));

			if($number=='ok'){

				$this->error('备份完成！','/admin/'.CONTROLLER_NAME.'/index');

			}else{

				$refresh= '<p>正在备份数据<strong>'.$_GET['value'].'</strong>,禁止关闭浏览器或窗口!</p>';

				$this->error($refresh,'/admin/'.CONTROLLER_NAME.'/post/value/'.$number);
			}
		}else{
			$ids=GetCheckBox($_POST['table'],1);

			$backup->SaveTable(explode(',',$ids),$_POST['backupname'],intval($_POST['backupfilesize']));
		
			$this->error('<p>正在备份数据表,禁止关闭浏览器或窗口!</p>','/admin/'.CONTROLLER_NAME.'/post/value/1');
		}
							   
	}
	
}